using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Patterns.Tools.CodeGenerators.GoogleCode.Namespaces
{
	public class NamespaceModel : WikiFolderModel
	{
		private static readonly Regex _parentPattern = new Regex(@"(?<parent>.+)([\.]+[^\.]+)?$", RegexOptions.Compiled);

		public override void Initialize()
		{
			SubNamespaces = Namespaces.Where(item => item.Length > Target.Length && item.StartsWith(Target));
			Parent = _parentPattern.Match(Target).Groups["parent"].Value;
			string parentWikiName = HasParentNamespace ? WikiNames.Namespace(Parent) : WikiNames.Assembly(Parent);
			ParentWikiUri = string.Format("{0}/{1}", BaseWikiUri, parentWikiName);
			HasParentNamespace = Namespaces.Any(item => StringComparer.OrdinalIgnoreCase.Equals(item, Parent));
			TypeLinks = Types.OrderBy(item => item.FullName).ToDictionary(item => item.FullName, item => GetWikiUri(WikiNames.Get(item)));
			SubNamespaceLinks = SubNamespaces.OrderBy(item => item).ToDictionary(item => item, item => GetWikiUri(WikiNames.Namespace(item)));
		}

		public IEnumerable<Type> Types{ get; set; }

		public IEnumerable<string> Namespaces { get; set; }

		public IEnumerable<string> SubNamespaces{ get; private set; }

		public bool HasParentNamespace { get; private set; }

		public string ParentWikiUri { get; private set; }

		public string Parent{ get; private set; }

		public IDictionary<string, string> TypeLinks { get; private set; }

		public IDictionary<string, string> SubNamespaceLinks { get; private set; }

		public string TypeIndent(string type)
		{
			throw new NotImplementedException();
		}

		public string NamespaceIndent(string namespaceName)
		{
			throw new NotImplementedException();
		}
	}
}